package apps.sim.core;

import java.util.HashSet;
import java.util.Set;

public class Trapping {
	public static class Contexthandler {
		private IHandler handler;
		private String context;

		public Contexthandler(IHandler handler, String context) {
			this.handler = handler;
			this.context = context;
		}

		public String getContext() {
			return context;
		}

		public IHandler getHandler() {
			return handler;
		}

		@Override
		public boolean equals(Object obj) {
			if (obj instanceof Contexthandler) {
				Contexthandler handler = (Contexthandler) obj;
				if (handler != null && handler.context != null) {
					return handler.context.equalsIgnoreCase(this.context);
				}
			}
			return super.equals(obj);
		}

		@Override
		public int hashCode() {
			return this.context != null ? this.context.hashCode() : 1;
		}
	}

	private String host;
	private int port;
	private boolean active;
	private Set<Contexthandler> contexts = new HashSet<Contexthandler>();
	private String tmpContext;

	public Trapping(String host, int port) {
		this.host = host;
		this.port = port;
	}

	public Trapping(String key) {
		final String[] split = key.split("//");
		final String[] split2 = split[1].split(":");
		this.host = split2[0];
		final int indexOf = split2[1].indexOf("/");
		this.port = Integer.parseInt(split2[1].substring(0, indexOf));
		setContext("/" + split2[1].substring(indexOf + 1));
	}

	public Set<Contexthandler> getContexts() {
		return contexts;
	}
	public String getTmpContext() {
		return tmpContext;
	}
	public String getHost() {
		return host;
	}

	public int getPort() {
		return port;
	}

	public void addContext(Contexthandler context) {
		this.contexts.add(context);
	}

	public void addContexts(Set<Contexthandler> contexts) {
		this.contexts.addAll(contexts);
	}

	public boolean forHost(String host) {
		return this.host.equalsIgnoreCase(host);
	}

	public boolean forPort(int port) {
		return this.port == port;
	}

	@Override
	public String toString() {
		return "ServerConfiguration [host=" + host + ", port=" + port + ", contexts=" + contexts + "]";
	}

	public void setContext(String context) {
		this.tmpContext = context;
	}

	public void setHandler(IHandler handler) {
		final Contexthandler ex = new Contexthandler(handler, this.tmpContext);
		if (this.contexts.contains(ex)) {
			this.contexts.remove(ex);
		}
		this.contexts.add(ex);
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Trapping) {
			Trapping trapping = (Trapping) obj;
			return trapping.getHost().equals(this.getHost()) && trapping.getPort() == this.getPort();
		}
		return super.equals(obj);
	}

	@Override
	public int hashCode() {
		return 1;
	}

	public String getURL() {
		for (Contexthandler contexthandler : getContexts()) {
			return "http://" + getHost() + ":" + getPort() + contexthandler.getContext();
		}
		return "http://" + getHost() + ":" + getPort();

	}
}
